package com.netease.task.controller.comm;


import com.netease.task.meta.Book;
import com.netease.task.meta.Order;
import com.netease.task.service.impl.BookServiceImpl;
import com.netease.task.service.impl.OrderServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.List;


@Controller
public class commOperation {

    @Autowired
    private BookServiceImpl bookService;

    @Autowired
    private OrderServiceImpl orderService;

    // 首页
    @RequestMapping({"/index","/"})
    public String index(Model model){
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        model.addAttribute("flag",0);
        List<Book> books = bookService.getBooks();
        model.addAttribute("books",books);
        return "index";
    }

    // 跳转到登录
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    // 登录
    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try{

            subject.login(token);  // 执行登录的方法,会去执行UserRealm类中的认证方法,认证成功则进行下一步,不成功会跳转到/toLogin页面
            // 系统只有两个用户: seller 和 buyer
            if("seller".equals(username)){
                model.addAttribute("flag",1);
                model.addAttribute("msg","卖家你好,seller!");
                return "api/sellerindex";
            } else {
                model.addAttribute("flag",2);
                model.addAttribute("msg","买家你好,buyer!");
                return "api/buyerindex";
            }
        }catch (UnknownAccountException | IncorrectCredentialsException e){
            // 用户名不存在
            /*model.addAttribute("msg","用户名或密码错误");*/
            return "login";
        }

    }

    // 登出
    @RequestMapping("/logout")
    public String logout(){
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    // 卖家首页
    @RequestMapping("/api/sellerindex")
    public String sellerIndex(Model model){
        List<Book> books = bookService.getBooks();
        model.addAttribute("books",books);
        return "/user/sellerindex";
    }

    // 买家首页
    @RequestMapping("/api/buyerindex")
    public String buyerindex(Model model){
        List<Book> books = bookService.getBooks();
        model.addAttribute("books",books);
        return "/user/buyerindex";
    }

    @RequestMapping("/api/toAccount")
    public String account(Model model){
        List<Order> orders = orderService.getOrderByType(1);   // 已完成的订单
        BigDecimal totalPrice = new BigDecimal("0");
        for (Order order : orders){
            totalPrice = totalPrice.add(order.getBuyPrice().multiply(new BigDecimal(order.getBuyNum())));
        }
        model.addAttribute("orders",orders);
        model.addAttribute("totalPrice",totalPrice);
        return "/user/account";
    }

    @RequestMapping("/api/toSettleAccount")
    public String settleAccount(Model model){
        List<Order> orders = orderService.getOrderByType(0);  // 未完成的订单,也就是购物车的物品
        model.addAttribute("orders",orders);
        return "/user/settleAccount";
    }


    // 未授权页面
    @RequestMapping("/noauthc")
    public String noauthc(){
        return "/noauthc";
    }



}
